package com.ufgov.agcfs.manage.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ufgov.agcfs.baseData.entity.ReportType;
import com.ufgov.agcfs.manage.dao.CompanyDao;
import com.ufgov.agcfs.manage.entity.Company;
import com.ufgov.agcfs.manage.entity.CompanyNode;
import com.ufgov.agcfs.pub.UUID;
import com.ufgov.agcfs.report.entity.RptTypeNode;

@Service
public class CompanyService {
	
	@Autowired
	public CompanyDao companyDao;
	
	public List<Company> getCompanyList(Map<String,String> condHash){
		return companyDao.getCompanyList(condHash);
	}
	
	public List<CompanyNode> getCompanyNodes(){
		List<CompanyNode> nodes = new ArrayList<CompanyNode>();
		List<Company> companyList = companyDao.findAllCompany();
		String coId="";
		String coCode="";
		String coName="";
		String parentCoId="";
		String coFullName="";
		Integer dispOrder=0;
		for (Company company : companyList) {
			coId = company.getCoId();
			parentCoId = company.getParentCoId();
			coName = company.getCoName();
			coCode = company.getCoCode();
			coFullName = company.getCoFullName();
			dispOrder = company.getDispOrde();
			CompanyNode node = CompanyNode.createNode(coId, parentCoId, coName, coCode, coFullName, dispOrder);
			nodes.add(node);
		}
		return nodes;
	}

	public List<CompanyNode> getCompanyByParentCoId(String parentCoId){
		List<CompanyNode> nodes = new ArrayList<CompanyNode>();
		List<Company> companyList = companyDao.findAllCompany();
		for (Company company : companyList) {
			if (company.getParentCoId().equals(parentCoId)){
				CompanyNode node = CompanyNode.createNode(company.getCoId(), parentCoId, company.getCoName(), 
						company.getCoCode(), company.getCoFullName(), company.getDispOrde());
				nodes.add(node);
			}
		}
		return nodes;
	}
	
	public void saveCompany(Company company,Map<String,String> resultMap){
		if ((company.getCoId()==null)||(company.getCoId().equals(""))) {
			Map<String,String> searchMap = new HashMap<String, String>();
			searchMap.put("coCode", company.getCoCode());
			if (companyDao.getCompanyList(searchMap).size()>0){
				getErrorMsg(resultMap,"saveError","单位已存在，请修改单位信息！");
				return;
			}
			company.setCoId(UUID.randomUUID().toString());
			companyDao.insertCompany(company);
		} else {
			Map<String,String> searchMap = new HashMap<String, String>();
			searchMap.put("coId",company.getCoId());
			Company com = companyDao.getCompanyList(searchMap).get(0);
			if (com.equals(company)){
				companyDao.updateCompany(company);
			} else {
				getErrorMsg(resultMap,"saveError","单位信息与系统中不一致！");
			}
		}	
	}
	
	private void getErrorMsg(Map<String, String> resultMap,String flg, String reason) {
		resultMap.put("result", "fail");
		resultMap.put(flg, reason);
		resultMap.put("reason", "保存失败！");
	}
}
